# -*- coding:utf-8 -*-

"""
version: 1.00.01
change: 2010-09-08 添加高级应用数据结构
"""

#定义私有配置文件数据结构
class Profile:
    def __init__(self):
        """
        初始化
        """
        #ROOT节点配置
        self.mem_max_used = 80
        self.mem_free_used = 60
        self.default_max_file_num = 1000
        self.default_max_node_num = 5
        self.default_max_over_num = 3
        #self.is_write_statlog = True
        
        #RECV节点
        self.recv_keyword = "/dev/shm/Ferry/RECV_KeyWord"
        self.recv_keyword_max_size = 1
        self.recv_keyword_max_num = 1
        self.recv_keyword_thr_num = 1
        self.recv_keyword_enable = 0

        self.recv_mass = "/dev/shm/Ferry/RECV_Mass"
        self.recv_mass_max_size = 1
        self.recv_mass_max_num = 1
        self.recv_mass_thr_num = 1
        self.recv_mass_enable = 0

        self.recv_bigdata = "/dev/shm/Ferry/RECV_Bigdata"
        self.recv_bigdata_max_size = 1
        self.recv_bigdata_max_num = 1
        self.recv_bigdata_thr_num = 1
        self.recv_bigdata_enable = 0

        self.recv_zip = "/dev/shm/Ferry/RECV_Zip"
        self.recv_zip_thr_num = 1
        self.unzip_bigdata_start = "06:00"
        self.unzip_bigdata_end = "23:00"

        #SEND节点
        self.send_keyword = "/dev/shm/Ferry/SEND_KeyWord"
        self.send_keyword_tmp = "/dev/shm/Ferry/SEND_KeyWord/Temp"
        self.send_mass = "/dev/shm/Ferry/SEND_Mass"
        self.send_mass_tmp = "/dev/shm/Ferry/SEND_Mass/Temp"
        self.send_bigdata = "/dev/shm/Ferry/SEND_Bigdata"
        self.send_bigdata_tmp = "/dev/shm/Ferry/SEND_Bigdata/Temp"
        self.send_zip = "/dev/shm/Ferry/RECV_Zip"
        self.send_zip_tmp = "/dev/shm/Ferry/RECV_Zip/Temp"

        #CACHE节点
        self.cache_dir = "/dev/shm/Ferry/Cache"
        
        #BACKUP节点
        self.backup_dir = "/backup"

        #日志备份数
        self.time_rotate_backup_num = 3
        self.size_rotate_backup_num = 0
        self.log_dir = "../log"
        self.log_lev = "DEBUG"

    def show(self):
        """
        打印私有配置文件信息
        """
        #RECEIVE节点
        print "---- receive section information ----"
        print "Keyword_Recv_Dir:", self.recv_keyword
        print "Keyword_Recv_Max_Time:", self.recv_keyword_max_time
        print "Keyword_Recv_Max_Size:", self.recv_keyword_max_size
        print "Keyword_Recv_Thread_Num:", self.recv_keyword_thr_num
        print "Keyword_Recv_Enable:", self.recv_keyword_enable

        print "Mass_Recv_Dir:", self.recv_mass
        print "Mass_Recv_Max_Time:", self.recv_mass_max_time
        print "Mass_Recv_Max_Size:", self.recv_mass_max_size
        print "Mass_Recv_Thread_Num:", self.recv_mass_thr_num
        print "Mass_Recv_Enable:", self.recv_mass_enable

        print "Bigdata_Recv_Dir:", self.recv_bigdata
        print "Bigdata_Recv_Max_Time:", self.recv_bigdata_max_time
        print "Bigdata_Recv_Max_Size:", self.recv_bigdata_max_size
        print "Bigdata_Recv_Thread_Num:", self.recv_bigdata_thr_num
        print "Bigdata_Recv_Enable:", self.recv_bigdata_enable

        print "Zip_Recv_Dir:", self.recv_zip
        print "Zip_Recv_Thread_Num:", self.recv_zip_thr_num

        # SEND节点
        print "---- send section information ----"
        print "Keyword_Send_Dir:", self.send_keyword
        print "Keyword_Send_Dir_Tmp:", self.send_keyword_tmp
        print "Mass_Send_Dir:", self.send_mass
        print "Mass_Send_Dir_Tmp:", self.send_mass_tmp
        print "Bigdata_Send_Dir:", self.send_bigdata
        print "Bigdata_Send_Dir_Tmp:", self.send_bigdata_tmp
        print "Zip_Send_Dir:", self.send_zip
        print "Zip_Send_Dir_Tmp:", self.send_zip_tmp
        print "Bigdata_Start_Time:", self.unzip_bigdata_start
        print "Bigdata_End_Time:", self.unzip_bigdata_end

        #CACHE节点
        print "---- cache section information ----"
        print "Cache_Dir:", self.cache_dir
        
        #BACKUP节点
        print "---- backup section information ----"
        print "Backup_Dir:", self.backup_dir

        #LOG节点
        print "---- log section information ----"
        print "SIZE_ROTATE_BACKUP_NUM:", self.size_rotate_backup_num
        print "TIME_ROTATE_BACKUP_NUM:", self.time_rotate_backup_num
        print "Log_Dir", self.log_dir
        print "Log_Level", self.log_lev

#定义规则数据结构
class RuleInfo:
    def __init__(self):
        """
        初始化
        """
        self.rule = ""
        self.ftp_list = []
        self.path_list = []
        
    def show(self):
        """
        输出规则信息
        """
        print self.rule , "=", ",".join([obj for obj in self.ftp_list]), " | ", ", ".join([obj for obj in self.path_list])

#定义PATH接口数据结构
class PathInfo:
    def __init__(self):
        """
        初始化
        """
        self.name = ""
        self.tmp_dir = ""
        self.dst_dir = ""
        self.adv_conf = []
        
    def show(self):
        """
        输出PATH接口信息
        """
        print "[" + self.name + "]"
        print "TMP_DIR =", self.tmp_dir
        print "DST_DIR =", self.dst_dir
        print "ADVANCED_CONF = ", ", ".join(self.adv_conf)

#定义FTP接口数据结构
class FTPInfo:
    def __init__(self):
        """
        初始化
        """
        self.name = ""
        self.ip_list = []
        self.port = 21
        self.username = ""
        self.password = ""
        self.tmp_dir = ""
        self.dst_dir = ""
        self.adv_conf = []
    
    def show(self):
        """
        输出FTP接口信息
        """
        print "[" + self.name + "]"
        print "SERVER_IP =", ",".join(self.ip_list)
        print "SERVER_PORT =", self.port
        print "SERVER_USERNAME =", self.username
        print "SERVER_PASSWORD =", self.password
        print "SERVER_TMP =", self.tmp_dir
        print "SERVER_DST =", self.dst_dir
        print "ADVANCED_CONF =", ", ".join(self.adv_conf)
        
    def equal(self, other):
        """
        判断两个FTP发送接口是否相同（节点名除外）
        @param other: FTP接口信息实例
        @return: True-完全相同，False-不相同
        """
        #判断FTP端口是否相同
        if self.port != other.port:
            return False
        
        #判断FTP用户名是否相同
        if self.username != other.username:
            return False
        
        #判断FTP用户密码是否相同
        if self.password != other.password:
            return False
        
        #判断临时目录是否相同
        if self.tmp_dir != other.tmp_dir:
            return False
        
        #判断目的目录是否相同
        if self.dst_dir != other.dst_dir:
            return False
        
        #判断IP列表是否相同
        self.ip_list.sort()
        other.ip_list.sort()
        if self.ip_list != other.ip_list:
            return False
            
        #判断高级应用是否相同
        self.adv_conf.sort()
        other.adv_conf.sort()
        if self.adv_conf != other.adv_conf:
            return False
    
        #FTP接口完全相同
        return True     
        
#定义重命名数据结构
class Rename:
    def __init__(self):
        """
        初始化
        """
        self.app_name = ""
        self.old_keyword = ""
        self.new_keyword = ""
    
    def show(self):
        """
        输出重命名信息
        """
        print "[" + self.app_name + "]"
        print "OLD_KEYWORD =", self.old_keyword
        print "NEW_KEYWORD =", self.new_keyword
        
#定义自定义积压文件数数据结构
class OverStock:
    def __init__(self):
        """
        初始化
        """
        self.app_name = ""
        self.max_file_num = 1000
        self.max_node_num = 5
        self.max_over_num = 3
        
    def show(self):
        """
        输出自定义积压文件数信息
        """
        print "[" + self.app_name + "]"
        print "MAX_FILE_NUM =", self.max_file_num
        print "MAX_NODE_NUM =", self.max_node_num
        print "MAX_OVER_NUM =", self.max_over_num
        
#定义统计日志数据结构
class StatLog:
    def __init__(self):
        """
        初始化
        """
        self.app_name = ""
        self.data_dest = ""
        
    def show(self):
        """
        输出统计日志信息
        """
        print "[" + self.app_name + "]"
        print "DATA_DEST =", self.data_dest

#定义应用配置文件数据结构
class AppConf:
    def __init__(self):
        """
        初始化
        """
        self.match_rule_list = []
        self.nomatch_rule_list = []
        self.ftp_unit_list = []
        self.path_unit_list = []
        self.adv_conf_list = []
        
    def show(self):
        print "---- app config information ----"
        print "[RULE]"
        for match_rule in self.match_rule_list:
            match_rule.show()
        print "[-RULE]"
        for match_rule in self.nomatch_rule_list:
            match_rule.show()
        for ftp_unit in self.ftp_unit_list:
            ftp_unit.show()
        for path_unit in self.path_unit_list:
            path_unit.show()
        for adv_conf in self.adv_conf_list:
            adv_conf.show()
        
            


            
